var datagrid = null;
var costidArr = null;
var projectId = null;
//插入or修改标志:0,新增 1,修改
var upsert = 0;
var costid = null;
var verifyid = null;

$(function() {
	initDialog();
	initMsgDatagird();
	$("#addCostA").bind("click", function(){
		upsert = 0;
		emptyForm();	
		$("#gmtRefund").datepicker("setDate", new Date());  
		$("#ui-dialog-title-userDialog").text("新增报销（单位：元）");
		readonlyForm("costForm", false);
		$("#opearDIV").show();
		$("#addLeaderCost").show();
		$("#addOutputvalueCost").show();
		$("#projectId").attr("readonly",false); 
		MZ.openDialog("addCostDialog");
	});
	$("#seeCostA").bind("click", function(){
		//reinit
		var rowVals = datagrid.getRowValues();
		if(rowVals.length == 1){
			var rowVal = rowVals[0];
			//clear form
			emptyForm();
			//load cost data
			loadCost(rowVal);
			$('#leaderCostField li a[name="delOther"]').each(function(i){
				$("#leaderCostLI"+(i+1)+" a[name='delOther']").hide();
			});
			$('#outputvalueCostField li a[name="delOther"]').each(function(i){
				$("#outputvalueCostLI"+(i+1)+" a[name='delOther']").hide();
			});
			
			$("#ui-dialog-title-addCostDialog").text("查看报销（单位：元）");
			$("#opearDIV").hide();
			$("#addLeaderCost").hide();
			$("#addOutputvalueCost").hide();
			readonlyForm("costForm", true);
			MZ.openDialog("addCostDialog");
		}else{
			MZ.showMsg("请选择一行记录查看!");
		}
	});
	//for update cost click event
	$("#updateCostA").bind("click", function(){
		//reinit
		upsert = 1;
		costid = null;
		verifyid = null;
		var rowVals = datagrid.getRowValues();
		if(rowVals.length == 1){
			var rowVal = rowVals[0];
			var status = rowVal.status;
			//set cost id
			costid = rowVal.id;
			verifyid = rowVal.verifyId;
			if("未通过"== status){
				MZ.showMsg("被驳回的审核不能再修改!");
				return false;
			}
			if("通过"== status){
				MZ.showMsg("已通过的审核不能再修改!");
				return false;
			}
			if("审核取消"== status){
				MZ.showMsg("已取消的审核不能再修改!");
				return false;
			}
			if("审核中"== status){
				MZ.showMsg("审核中的数据不能修改!");
				return false;
			}
			if("已提交"== status){
				MZ.showMsg("已提交的数据不能修改!");
				return false;
			}
			//clear form
			emptyForm();
			//load cost data
			loadCost(rowVal);
			$("#ui-dialog-title-addCostDialog").text("修改报销（单位：元）");
			$("#opearDIV").show();
			$("#addLeaderCost").show();
			$("#addOutputvalueCost").show();
			readonlyForm("costForm", false);
			MZ.openDialog("addCostDialog");
		}else{
			MZ.showMsg("请选择一行记录修改!");
		}
	});
	$("#submitCostA").bind("click", function(){
		costid = null;
		verifyid = null;
		var rowVals = datagrid.getRowValues();
		if(rowVals.length == 1){
			var rowVal = rowVals[0];
			var status = rowVal.status;
			//set cost id
			costid = rowVal.id;
			verifyid = rowVal.verifyId;
			if("通过"== status){
				MZ.showMsg("已通过的审核不需要再提交!");
				return false;
			}
			if("审核取消"== status){
				MZ.showMsg("已取消的审核不能再提交!");
				return false;
			}
			if("审核中"== status){
				MZ.showMsg("审核中的数据不需要再提交!");
				return false;
			}
			if("已提交"== status){
				MZ.showMsg("已提交的数据不需要再提交!");
				return false;
			}
			if("未通过"== status){
				MZ.showMsg("未通过的数据不能再提交!");
				return false;
			}
			jConfirm('确定提交该报销记录吗?', '系统提示', function(r) {
			    if(r){
			    	submitVerify();
			    }
			});
		}else{
			MZ.showMsg("请选择一行记录取消!");
		}
	});
	$("#cancelCostA").bind("click", function(){
		costidArr = null;
		costidArr = datagrid.getRowValue(2);
		if(costidArr.length >= 1){
			var status = datagrid.getRowValue(6);
			if(jQuery.inArray("未通过", status) != -1){
				MZ.showMsg("被驳回的审核不需要再取消!");
				return false;
			}
			if(jQuery.inArray("通过", status) != -1){
				MZ.showMsg("已通过的审核不能再取消!");
				return false;
			}
			if(jQuery.inArray("审核取消", status) != -1){
				MZ.showMsg("已取消的审核不需要再取消!");
				return false;
			}
			
			MZ.openDialog("dialog-confirm");
		}else{
			MZ.showMsg("请选择一行记录取消!");
		}
	});
	$("#delCostA").bind("click", function(){
		costid = null;
		verifyid = null;
		var rowVals = datagrid.getRowValues();
		if(rowVals.length == 1){
			var rowVal = rowVals[0];
			var status = rowVal.status;
			//set cost id
			costid = rowVal.id;
			verifyid = rowVal.verifyId;
			if("通过"== status){
				MZ.showMsg("已通过的审核不能删除!");
				return false;
			}
			if("审核中"== status){
				MZ.showMsg("审核中的数据不能删除!");
				return false;
			}
			jConfirm('确定删除该报销记录吗?', '系统提示', function(r) {
			    if(r){
			    	delVerify();
			    }
			});
		}else{
			MZ.showMsg("请选择一行记录删除!");
		}
	});
	
	$("#projectId").autocomplete('/project/newproject/autoFillProjct', {
		minChars: 0,
		width: 450,
		matchContains: false,
		autoFill: true,
		mustMatch: true,
		dataType: 'json',
		//加入对返回的json对象进行解析函数，函数返回一个数组      
		parse: function(data) {  
			var rows = [];  
		    for(var i=0; i<data.length; i++){  
		    	rows[i] = {   
		    		 data:data[i].id +"-"+data[i].name,   
		    		 value:data[i].id,   
		    		 result:data[i].name   
		        };   
		      }  
		    return rows;  
		},  
		formatItem: function(row, i, n) {
		    return row;        
		}
//		formatItem: function(row, i, max) {
//			return row[0];
//		},
//		formatMatch: function(row, i, max) {
//			return row[0];
//		},
//		formatResult: function(row) {
//			return row;
//		}
	}).result(function(event, data, formatted) {
		projectId = null;
		if(typeof(data)!="undefined" && data.indexOf("-")!=-1){
			projectId = data.split("-")[0];
		}
	});
	//for leader cost
	$('#addLeaderCost').bind('click',function(){
		var liSize = $("#leaderCostField li").length;
		var otherCost=$('#leaderCostLI1').clone(true);
		otherCost.insertBefore('#addLeaderCostLI');
		otherCost.attr("id","leaderCostLI"+liSize)
		$("#leaderCostLI"+liSize+" input[name='leaderCost']").val("");
		$('#leaderCostField li a[name="delOther"]').each(function(i){
			$("#leaderCostLI"+(i+1)+" a[name='delOther']").hide();
		});
		$("#leaderCostLI"+liSize+" a[name='delOther']").show();
		otherCost.show();
	});
	
	$('#leaderCostField a[name="delOther"]').bind('click',function(){
		var liSize = $("#leaderCostField li").length-1;
		//update totalCurrency
		var leaderCostVal = $("#leaderCostLI"+liSize+" input[name='leaderCost']").val();
		leaderCostVal = $.trim(leaderCostVal) * 1;
		if(leaderCostVal != ""){
			if($("#totalCurrencySPAN").text() != ""){
				var totalVal = ($("#totalCurrencySPAN").text() * 1) - leaderCostVal;
				totalVal = MZ.toDecimal(totalVal, MZ.CURRENCY_ROUND);
				$("#totalCurrencySPAN").text(totalVal);
			}
		}
		var otherCost=$(this).parents("li");
		otherCost.remove();
		liSize = $("#leaderCostField li").length-1;
		if(liSize != 1){
			$("#leaderCostLI"+liSize+" a[name='delOther']").show();
		}
	});
	//for output value cost
	$('#addOutputvalueCost').bind('click',function(){
		var liSize = $("#outputvalueCostField li").length;
		var otherCost=$('#outputvalueCostLI1').clone(true);
		otherCost.insertBefore('#addOutputvalueCostLI');
		otherCost.attr("id","outputvalueCostLI"+liSize)
		$("#outputvalueCostLI"+liSize+" input[name='outputvalueCost']").val("");
		$('#outputvalueCostField li a[name="delOther"]').each(function(i){
			$("#outputvalueCostLI"+(i+1)+" a[name='delOther']").hide();
		});
		$("#outputvalueCostLI"+liSize+" a[name='delOther']").show();
		otherCost.show();
	});
	
	$('#outputvalueCostField a[name="delOther"]').bind('click',function(){
		var liSize = $("#outputvalueCostField li").length-1;
		//update totalCurrency
		var costVal = $("#outputvalueCostLI"+liSize+" input[name='outputvalueCost']").val();
		costVal = $.trim(costVal) * 1;
		if(costVal != ""){
			if($("#totalCurrencySPAN").text() != ""){
				var totalVal = ($("#totalCurrencySPAN").text() * 1) - costVal;
				totalVal = MZ.toDecimal(totalVal, MZ.CURRENCY_ROUND);
				$("#totalCurrencySPAN").text(totalVal);
			}
		}
		var otherCost=$(this).parents("li");
		otherCost.remove();
		liSize = $("#outputvalueCostField li").length-1;
		if(liSize != 1){
			$("#outputvalueCostLI"+liSize+" a[name='delOther']").show();
		}
	});
	
	$('#addOtherCost').bind('click',function(){
		var liSize = $("#othercostField li").length;
		var otherCost=$('#othercostLI1').clone(true);
		otherCost.insertBefore('#addOtherCostLi');
		otherCost.attr("id","othercostLI"+liSize)
		$("#othercostLI"+liSize+" input[name='otherCost']").val("");
		$('#othercostField li a[name="delOther"]').each(function(i){
			$("#othercostLI"+(i+1)+" a[name='delOther']").hide();
		});
		$("#othercostLI"+liSize+" a[name='delOther']").show();
		otherCost.show();
	});
	
	$('#othercostField a[name="delOther"]').bind('click',function(){
		var liSize = $("#othercostField li").length-1;
		//update totalCurrency
		var otherCostVal = $("#othercostLI"+liSize+" input[name='otherCost']").val();
		otherCostVal = $.trim(otherCostVal) * 1;
		if(otherCostVal != ""){
			if($("#totalCurrencySPAN").text() != ""){
				var totalVal = ($("#totalCurrencySPAN").text() * 1) - otherCostVal;
				totalVal = MZ.toDecimal(totalVal, MZ.CURRENCY_ROUND);
				$("#totalCurrencySPAN").text(totalVal);
			}
		}
		var otherCost=$(this).parents("li");
		otherCost.remove();
		liSize = $("#othercostField li").length-1;
		if(liSize != 1){
			$("#othercostLI"+liSize+" a[name='delOther']").show();
		}
		
	});
	
	//init datapicker
	$("#gmtRefund").datepicker("option", "defaultDate", new Date());  
	$("#gmtRefund").datepicker("option", "maxDate", new Date());
	
	$("#submitBtn").bind("click", function(){
		if(upsert == 0){
			addCost();
		}else if(upsert == 1){
			updateCost();
		}
	});
	$("#resetBtn").bind("click", function(){
		MZ.clearForm("costForm");
	});
	
	$("#searchA").bind("click", function(){
		searchCost();
	});
	
	$("#resetA").bind("click", function(){
		$("#projectNameText").val("");
	});
	
	//绑定金额输入框keyup事件
	//当输入框数量more时，需要采用total-old+new更新total值
	//不宜再全局运算
	$("#costForm input.currency-type").keyup(function(event){ 
		//console.log("oldVal in key up: %d", oldVal);
		var reg = /^-?\d+\.{0,}\d{0,}$/;
		if(!reg.test($(this).val())){
			$(this).val("");
		}
		var totalVal = 0;
		$("#costForm input.currency-type").each(function(){
			totalVal += $(this).val()*1;
		});
		totalVal = MZ.toDecimal(totalVal, MZ.CURRENCY_ROUND);
		$("#totalCurrencySPAN").text(totalVal);
	});
	
});

function initDialog(){
	//DIALOG//
	$('#addCostDialog').dialog({
		resizable: false,
		autoOpen: false,
		width: 600,
		height: 500,
		modal: true
	});
	
	$( "#dialog-confirm" ).dialog({
		resizable: false,
		autoOpen: false,
		height:180,
		modal: true,
		buttons: {
			"确认删除": function() {
				cancalVerify();
				$( this ).dialog( "close" );
			},
			"取消": function() {
				$( this ).dialog( "close" );
			}
		}
	});
}
//init message datagird
//get json data from server
function initMsgDatagird(){
	datagrid = $("#msgDatagird").flexigrid({
		url: "companycost/getCompanyCost",
		colModel : [
	        {display: "ID", name : "id", width : 90, sortable : true, hide: true},
			{display: "项目", name : "projectName", width : 400, sortable : true},
			{display: "报销人", name : "refundUser", width : 90, sortable : true},
			{display: "报销时间", name : "gmtRefund", width : 90, sortable : true, process:formatTime},
			{display: "审核状态", name : "status", width : 90, sortable : true, process:formatStatus},//process:formatStatus
			{display: "审核人", name : "verifierName", width : 90, sortable : true},
			{display: "VID", name : "verifyId", width : 90, sortable : true, hide: true},
			{display: "工资/奖金", name : "wageCost", width : 90, sortable : true, process:formatCost},
			{display: "经营部差旅", name : "businessdeptCost", width : 90, sortable : true, process:formatCost},
			{display: "总工办差旅", name : "chiefengCost", width : 90, sortable : true, process:formatCost},
			{display: "领导差旅", name : "leaderCost", width : 90, sortable : true, process:formatCost},
			{display: "LDetail", name : "leaderDetail", width : 90, sortable : true, hide: true},
			{display: "经营招待费", name : "entertainCost", width : 90, sortable : true, process:formatCost},
			{display: "领导招待费", name : "leaderentCost", width : 90, sortable : true, process:formatCost},
			{display: "会务费", name : "conferenceCost", width : 90, sortable : true, process:formatCost},
			{display: "CDetail", name : "conferenceDetail", width : 90, sortable : true, hide: true},
			{display: "劳务费", name : "laborCost", width : 90, sortable : true, process:formatCost},
			{display: "招投标费", name : "biddingCost", width : 90, sortable : true, process:formatCost},
			{display: "出版装订", name : "publishCost", width : 90, sortable : true, process:formatCost},
			{display: "产值管理", name : "outputvalueCost", width : 90, sortable : true, process:formatCost},
			{display: "ODetail", name : "outputvalueDetail", width : 90, sortable : true, hide: true},
			{display: "设备费", name : "equCost", width : 90, sortable : true, process:formatCost},
			{display: "合计", name : "totalCost", width : 120, sortable : true, process:formatCost}
		]
    });
}

//funciton onSelectRow required
//update flexigrid onselectrow function
//if you need't use onselect row
//let this function blank to do nothing
function onSelectRow(tr){
	
}

function formatTime(tdDiv, pid){
	return $(tdDiv).text().substr(0, 10);
}
//格式化STATUS显示
function formatStatus(tdDiv, pid){
	var status = $(tdDiv).text();
	if(status == -1){
		return "<font color='red'>未通过</font>";
	}else if(status == 1){
		return "<font color='blue'>审核中</font>";
	}else if(status == 2){
		return "<font color='green'>通过</font>";
	}else if(status == null){
		return "<font color='orange'>未提交</font>";
	}else if(status == 4){
		return "<font color='black'>审核取消</font>";
	}else if(status == 0){
		return "<font color='orange'>未提交</font>";
	}else{
		return "<font color='purple'>未知</font>";
	}
}
function formatCost(tdDiv, pid){
	var cost = $(tdDiv).text();
	if(cost == "0"){
		return "";
	}
	return MZ.toDecimal(cost / MZ.CURRENCY_PRECISION , MZ.CURRENCY_ROUND);
}

function searchCost(){
	var projectName = $("#projectNameText").val();
	var params = [{name: "projectName", value: projectName}];
	$("#msgDatagird").flexOptions({newp: 1, params: params}).flexReload();
}

function addCost(){
	var params = $('#costForm').serializeObject();
	params["projectId"] = Number(projectId);
	
	var costDataArr = new Array();
	var dictName = "";
	var liId = null;
	var cost = null;
	
	var leaderCost = 0;
	$("#leaderCostField input[name='leaderName']").each(function(i){
		if($(this).val() != null || $(this).val() != ""){
			liId = $(this).parents("li").attr("id");
			cost = $("#"+liId+" input[name='leaderCost']").val() * 1;
			leaderCost += cost * 1;
			name = $(this).val();
			costDataArr.push("{'cost':"+cost+",'id':'"+name+"','name':'"+name+"'}");
		}
	});
	//console.log("leaderCost = %d", leaderCost);
	//console.log("leaderCost * MZ.CURRENCY_PRECISION = %d", leaderCost * MZ.CURRENCY_PRECISION);
	params["leaderCost"] = leaderCost;
	params["leaderDetail"] = "["+costDataArr.toString()+"]";
	
	costDataArr = new Array();
	var outputvalueCost = 0;
	$("#outputvalueCostField select[name='outputvalueCost']").each(function(i){
		if($(this).val() != null || $(this).val() != ""){
			liId = $(this).parents("li").attr("id");
			cost = $("#"+liId+" input[name='outputvalueCost']").val() * 1;
			outputvalueCost += cost * 1;
			dictName = $(this).find("option:selected").text();
			costDataArr.push("{'cost':"+cost+",'id':"+$(this).val()+",'name':'"+dictName+"'}");
		}
	});
	params["outputvalueCost"] = outputvalueCost;
	params["outputvalueDetail"] = "["+costDataArr.toString()+"]";
	
	costDataArr = new Array();
	var otherCost = 0;
	$("#othercostField select[name='otherCost']").each(function(i){
		if($(this).val() != null || $(this).val() != ""){
			liId = $(this).parents("li").attr("id");
			cost = $("#"+liId+" input[name='otherCost']").val() * 1;
			otherCost += cost * 1;
			dictName = $(this).find("option:selected").text();
			costDataArr.push("{'cost':"+cost+",'id':"+$(this).val()+",'name':'"+dictName+"'}");
		}
	});
	params["conferenceCost"] = otherCost;
	params["conferenceDetail"] = "["+costDataArr.toString()+"]";
	
	for(var key in params){
		if(key.indexOf("Cost") != -1){
			//console.log("params[%s] = %d", key, params[key]);
			params[key] = MZ.toDecimal(params[key] * MZ.CURRENCY_PRECISION, MZ.CURRENCY_ROUND);
		}
	}
//	var gmtRefund = $("#gmtRefund").datepicker( "getDate" )
//	params["gmtRefund"] = gmtRefund;
	if(!validForm(params)){
		return false;
	}
	params["leaderName"] = "";
	params = $.toJSON(params);
//	MZ.showMsg(params);
	$.ajax({
		url : "companycost/addCompanyCost",
//		processData: false,
		type : "post",
		data : "data="+params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("addCostDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

//add by zoe 2012.07.08
//for update,delete,submit cost
function updateCost(){
	var params = $('#costForm').serializeObjectWithZero();
	params["projectId"] = Number(projectId);
	
	var costDataArr = new Array();
	var dictName = "";
	var liId = null;
	var cost = null;
	
	var leaderCost = 0;
	$("#leaderCostField input[name='leaderName']").each(function(i){
		if($(this).val() != null || $(this).val() != ""){
			liId = $(this).parents("li").attr("id");
			cost = $("#"+liId+" input[name='leaderCost']").val() * 1;
			leaderCost += cost * 1;
			name = $(this).val();
			costDataArr.push("{'cost':"+cost+",'id':'"+name+"','name':'"+name+"'}");
		}
	});
	//console.log("leaderCost = %d", leaderCost);
	//console.log("leaderCost * MZ.CURRENCY_PRECISION = %d", leaderCost * MZ.CURRENCY_PRECISION);
	params["leaderCost"] = leaderCost;
	params["leaderDetail"] = "["+costDataArr.toString()+"]";

	costDataArr = new Array();
	var outputvalueCost = 0;
	$("#outputvalueCostField select[name='outputvalueCost']").each(function(i){
		if($(this).val() != null || $(this).val() != ""){
			liId = $(this).parents("li").attr("id");
			cost = $("#"+liId+" input[name='outputvalueCost']").val() * 1;
			outputvalueCost += cost * 1;
			dictName = $(this).find("option:selected").text();
			costDataArr.push("{'cost':"+cost+",'id':"+$(this).val()+",'name':'"+dictName+"'}");
		}
	});
	params["outputvalueCost"] = outputvalueCost;
	params["outputvalueDetail"] = "["+costDataArr.toString()+"]";
	
	
	costDataArr = new Array();
	var otherCost = 0;
	$("#othercostField select[name='otherCost']").each(function(i){
		if($(this).val() != null || $(this).val() != ""){
			liId = $(this).parents("li").attr("id");
			cost = $("#"+liId+" input[name='otherCost']").val() * 1;
			otherCost += cost * 1;
			dictName = $(this).find("option:selected").text();
			costDataArr.push("{'cost':"+cost+",'id':"+$(this).val()+",'name':'"+dictName+"'}");
		}
	});
	params["conferenceCost"] = otherCost;
	params["conferenceDetail"] = "["+costDataArr.toString()+"]";
	
	for(var key in params){
		if(key.indexOf("Cost") != -1){
			//console.log("params[%s] = %d", key, params[key]);
			params[key] = MZ.toDecimal(params[key] * MZ.CURRENCY_PRECISION, MZ.CURRENCY_ROUND);
		}
	}
	//	var gmtRefund = $("#gmtRefund").datepicker( "getDate" )
	//	params["gmtRefund"] = gmtRefund;
	params["id"] = costid;
	params["verifyId"] = verifyid;
	if(!validForm(params)){
		return false;
	}
	params["leaderName"] = "";
	params = $.toJSON(params);
//	MZ.showMsg(params);
	$.ajax({
		url : "companycost/updateCost",
//		processData: false,
		type : "post",
		data : "data="+params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("addCostDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function cancalVerify(){
	//cancalVerify
	var params = {"ids":costidArr.toString()};
	$.ajax({
		url : "companycost/cancalVerify",
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function submitVerify(){
	//submitVerify
	if(costid == null){
		MZ.showMsg("错误的记录ID");
		return false;
	}
	if (verifyid == null){
		MZ.showMsg("错误的记录ID");
		return false;
	}
	var params = {"id":costid, "vid": verifyid};
	$.ajax({
		url : "companycost/submitVerify",
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function delVerify(){
	//delVerify
	if(costid == null){
		MZ.showMsg("错误的记录ID");
		return false;
	}
	if (verifyid == null){
		MZ.showMsg("错误的记录ID");
		return false;
	}
	var params = {"id":costid, "vid": verifyid};
	$.ajax({
		url : "companycost/delVerify",
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

//params : json type
function validForm(params){
	var flag = true;
	//金额正则验证
	var reg = /^-?\d+\.{0,}\d{0,}$/;
	//PROJECT
	if($.trim(params.projectId) == "" || params.projectId == 0){
		MZ.showMsg("请选择一个项目!");
		return false;
	}
	if($.trim(params.verifier) == ""){
		MZ.showMsg("请指定一个审核人!");
		return false;
	}
	//refundUser
	if($.trim(params.refundUser) == ""){
		MZ.showMsg("报销人不能为空!");
		return false;
	}
	//gmtRefund
	if($.trim(params.gmtRefund) == ""){
		MZ.showMsg("报销时间不能为空!");
		return false;
	}
	//wageCost
	//keyup事件限制了金额输入，不再做金额验证...
	if($.trim(params.wageCost) != ""){
		if(!reg.test(params.wageCost)){
			MZ.showMsg("请输入正确的工资/奖金金额!");
			return false;
		}		
	}
	//验证总金额
	var totalCurrency = 0;
	$("#costForm input.currency-type").each(function(){
		totalCurrency += $(this).val()*1;
	});
	if(totalCurrency == 0){
		MZ.showMsg("未输入有效的报销项!");
		return false;
	}
	//leaderCost
	var liId = null;
	var cost = null;
	$("#leaderCostField input[name='leaderName']").each(function(i){
		liId = $(this).parents("li").attr("id");
		cost = $("#"+liId+" input[name='leaderCost']").val();
		if($(this).val() == null || $(this).val() == ""){
			if(i != 0){
				MZ.showMsg("未输入领导姓名!");
				flag = false;
			}else{
				if(cost != ""){
					MZ.showMsg("未输入领导姓名!");
					flag = false;
				}
			}
		}else{
			if(cost == ""){
				MZ.showMsg("未输入有效的领导差旅报销项!");
				flag = false;
			}
		}
	});
	//outputvalue cost
	$("#outputvalueCostField select[name='outputvalueCost']").each(function(i){
		liId = $(this).parents("li").attr("id");
		cost = $("#"+liId+" input[name='outputvalueCost']").val();
		if($(this).val() == null || $(this).val() == ""){
			if(i != 0){
				MZ.showMsg("未选择有效的产值管理报销项!");
				flag = false;
			}else{
				if(cost != ""){
					MZ.showMsg("未选择有效的产值管理报销项!");
					flag = false;
				}
			}
		}else{
			if(cost == ""){
				MZ.showMsg("未输入有效的产值管理报销项!");
				flag = false;
			}
		}
	});
	//otherCost
	$("#othercostField select[name='otherCost']").each(function(i){
		liId = $(this).parents("li").attr("id");
		cost = $("#"+liId+" input[name='otherCost']").val();
		if($(this).val() == null || $(this).val() == ""){
			if(i != 0){
				MZ.showMsg("未选择有效的其他报销项!");
				flag = false;
			}else{
				if(cost != ""){
					MZ.showMsg("未选择有效的其他报销项!");
					flag = false;
				}
			}
		}else{
			if(cost == ""){
				MZ.showMsg("未输入有效的其他报销项!");
				flag = false;
			}
		}
	});
	return flag;
}

//LOAD COST INFO 2 FORM 4 UPDATE
function loadCost(jsonData){
	//costTable
	//set project id and name
	projectId = jsonData["projectId"];
	//total
	$("#totalCurrencySPAN").text(jsonData["totalCost"]);
	$("#projectId").val(jsonData["projectName"]);
	$("#projectId").attr("readonly",true); 
	for(var key in jsonData){
		$("#"+key).val(jsonData[key]);
	}
	//load verifyer
	$("#verifierSel option").each(function(){
		if($(this).text()==jsonData["verifierName"]){
			$(this).attr("selected","true");
		}
	});
	//load other cost
	var leaderDetail = jsonData['leaderDetail']
	if($.trim(leaderDetail) != ""){
		leaderDetail = eval( "(" + leaderDetail + ")" );
		var obj = null;
		for(var i in leaderDetail){
			obj = leaderDetail[i];
			if(i == 0){
				$("#leaderCostLI1 input[name='leaderName']").val(obj["name"]);
				$("#leaderCostLI1 input[name='leaderCost']").val(obj["cost"]);
			}else{
				var liSize = $("#leaderCostField li").length;
				var otherCost=$('#leaderCostLI1').clone(true);
				otherCost.insertBefore('#addLeaderCostLI');
				otherCost.attr("id","leaderCostLI"+liSize)
				$("#leaderCostLI"+liSize+" input[name='leaderName']").val(obj["name"]);
				$("#leaderCostLI"+liSize+" input[name='leaderCost']").val(obj["cost"]);
				otherCost.show();
			}
		}
		$('#leaderCostField li a[name="delOther"]').each(function(i){
			$("#leaderCostLI"+(i+1)+" a[name='delOther']").hide();
		});
		var liSize = $("#leaderCostField li").length - 1;
		$("#leaderCostLI"+liSize+" a[name='delOther']").show();
	}
	var oDetail = jsonData['outputvalueDetail']
	if($.trim(oDetail) != ""){
		oDetail = eval( "(" + oDetail + ")" );
		var obj = null;
		for(var i in oDetail){
			obj = oDetail[i];
			if(i == 0){
				$("#outputvalueCostLI1 select[name='outputvalueCost']").val(obj["id"]);
				$("#outputvalueCostLI1 input[name='outputvalueCost']").val(obj["cost"]);
			}else{
				var liSize = $("#outputvalueCostField li").length;
				var otherCost=$('#outputvalueCostLI1').clone(true);
				otherCost.insertBefore('#addOutputvalueCostLI');
				otherCost.attr("id","outputvalueCostLI"+liSize)
				$("#outputvalueCostLI"+liSize+" select[name='outputvalueCost']").val(obj["id"]);
				$("#outputvalueCostLI"+liSize+" input[name='outputvalueCost']").val(obj["cost"]);
				otherCost.show();
			}
		} 
		$('#outputvalueCostField li a[name="delOther"]').each(function(i){
			$("#outputvalueCostLI"+(i+1)+" a[name='delOther']").hide();
		});
		var liSize = $("#outputvalueCostField li").length - 1;
		$("#outputvalueCostLI"+liSize+" a[name='delOther']").show();
	}
	//load other cost
	var otherDetail = jsonData['conferenceDetail']
	if($.trim(otherDetail) != ""){
		otherDetail = eval( "(" + otherDetail + ")" );
		var obj = null;
		for(var i in otherDetail){
			obj = otherDetail[i];
			if(i == 0){
				$("#othercostLI1 select[name='otherCost']").val(obj["id"]);
				$("#othercostLI1 input[name='otherCost']").val(obj["cost"]);
			}else{
				var liSize = $("#othercostField li").length;
				var otherCost=$('#othercostLI1').clone(true);
				otherCost.insertBefore('#addOtherCostLi');
				otherCost.attr("id","othercostLI"+liSize)
				$("#othercostLI"+liSize+" select[name='otherCost']").val(obj["id"]);
				$("#othercostLI"+liSize+" input[name='otherCost']").val(obj["cost"]);
				otherCost.show();
			}
		} 
		$('#othercostField li a[name="delOther"]').each(function(i){
			$("#othercostLI"+(i+1)+" a[name='delOther']").hide();
		});
		var liSize = $("#othercostField li").length - 1;
		$("#othercostLI"+liSize+" a[name='delOther']").show();
	}
}

function readonlyForm(formId, flag){
	if(flag){
		$("#"+formId+" input").attr("readonly",true);
	}else{
		$("#"+formId+" input").attr("readonly",false);
	}
}

function emptyForm(){
	MZ.clearForm("costForm");
	var liSize = $("#leaderCostField li").length;
	for(i = 2; i<liSize; i++){
		$('#leaderCostLI'+i).remove();
	}
	liSize = $("#outputvalueCostField li").length;
	for(i = 2; i<liSize; i++){
		$('#outputvalueCostLI'+i).remove();
	}
}
